package com.pzhu.view;

import com.pzhu.pojo.User;
import com.pzhu.service.LoginService;
import com.pzhu.service.impl.LoginServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class MainServlet extends HttpServlet {
    LoginService loginService =new LoginServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        //设置请求编码格式
        req.setCharacterEncoding("utf-8");
        //设置响应编码格式
        resp.setContentType("text/html;charset=utf-8");
        //获取Session对象
        HttpSession hs=req.getSession();
        //获取Session对象中的数据，user键名存储的是u对象的值，强转成User对象类型
        //假如过了30分钟才访问show页面，Session已经销毁了，请求转发到ckl，重建Session装入user数据
        if(hs.getAttribute("userId")==null){
            //重定向
            resp.sendRedirect("/ss/lbc");
            return;
        }
        User u = (User) hs.getAttribute("userId");
        //响应处理结果
        resp.getWriter().write("<html>");
        resp.getWriter().write("<head>");
        resp.getWriter().write("</head>");
        resp.getWriter().write("<body>");
        //登录提示语，获取u对象里的用户名
        resp.getWriter().write("<h3>欢迎"+u.getUname()+"登录</h3>");
        resp.getWriter().write("<hr/>");
        resp.getWriter().write("<form action='show' method='get'>");
        resp.getWriter().write("<input type='submit' value='查看个人信息'>");
        resp.getWriter().write("</form>");
        resp.getWriter().write("</body>");
        resp.getWriter().write("</html>");

    }
}
